﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using JSC.Portal.Web.App.Modules.ContactUs.Services;
using JSC.Portal.Web.Controllers;
using JSC.Portal.Entities;
using JSC.Core;
using JSC.Core.Data;
using JSC.Portal.Module;
using NHibernate;
using JSC.Data.NHibernate;
using JSC.Portal.Web.App.Modules.ContactUs.Entities;

namespace JSC.Portal.Web.App.Modules.ContactUs
{
    [HandleError]
    public class ContactUsController : ModuleServicedController<ContactUsModule, IContactUsService>
    {
        public ActionResult Send(int sectionid, string title, string name, string email, 
            string phoneNumber, string subject, string message)
        {
            LoadModule(sectionid);
            Contact c = new Contact();
            using (ITransaction trans = NHibernateSession.Current.BeginTransaction())
            {
                c.Section = Section;
                c.Title = title;
                c.Name = name;
                c.EMail = email;
                c.PhoneNumber = phoneNumber;
                c.Subject = subject;
                c.Message = message;
                c.Moment = DateTime.Now;
                c = Service.SaveOrUpdate(c);
                trans.Commit();
            }
            Module.SendMail(c);

            return RedirectToAction("ShowFriendlyUrl", ControllerNames.PORTALENGINE_CONTROLLER, new { language = Section.Page.Language, friendlyurl = Section.Page.FriendlyURL });
        }
    }
}
